package com.ruoyi.project.digital.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.AjaxResult;

import com.ruoyi.project.common.component.AliOssProperties;
import com.ruoyi.project.digital.domain.Mood;
import com.ruoyi.project.digital.domain.TUser;
import com.ruoyi.project.digital.domain.WechatUser;
import com.ruoyi.project.digital.domain.dto.LoginDto;
import com.ruoyi.project.digital.domain.dto.LoginWechatUser;
import com.ruoyi.project.digital.domain.enums.IsInitEmum;
import com.ruoyi.project.digital.domain.enums.IsLineEmum;
import com.ruoyi.project.digital.domain.enums.YesOrNoStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 * 登录校验方法
 *
 * @author ruoyi
 */
@Component
public class WechatUserLoginService {
    @Autowired
    private WechatUserTokenService wechatUserTokenService;
    @Autowired
    private IWechatUserService wechatUserService;
    @Autowired
    private ITUserService userService;
    @Autowired
    private AliOssProperties ossProperties;
    @Autowired
    private  IMoodService moodService;

    /**
     * 登录验证
     * @param openid 唯一标识
     * @return 结果
     */
    public AjaxResult login(String openid) {
        if(ObjectUtil.isEmpty(openid)){
            return AjaxResult.error("OPENID参数空值");
        }
        TUser openIdCheck = userService.selectByOpenId(openid);
        if (null != openIdCheck) {
            LoginWechatUser loginWechatUser = new LoginWechatUser();
            BeanUtils.copyProperties(openIdCheck,loginWechatUser);
            // 生成token
            AjaxResult ajax = AjaxResult.success();
            ajax.put(Constants.TOKEN, wechatUserTokenService.createToken(loginWechatUser));
            //ajax.put(Constants.WECHAT_AUTH, openIdCheck.getUserType());
            return ajax;
        }else {
            TUser wechatUser = new TUser();
            wechatUser.setOpenId(openid);
            wechatUser.setBackGroup(ossProperties.getPreview()+Constants.DEAFULT_BACK_IMG);
            wechatUser.setIsInit(IsInitEmum.NO.getValue());
            wechatUser.setIsLine(IsLineEmum.YES.getValue());
            userService.insertTUser(wechatUser);
            LoginWechatUser loginWechatUser = new LoginWechatUser();
            BeanUtils.copyProperties(wechatUser,loginWechatUser);
            // 生成token
            AjaxResult ajax = AjaxResult.success();
            ajax.put(Constants.TOKEN, wechatUserTokenService.createToken(loginWechatUser));
            return ajax;
        }
    }

    /**
     * 按照用户名和Miami登录
     * @param loginDto
     * @return
     */
    public AjaxResult loginByUserName(LoginDto loginDto) {
        TUser user=   userService.getUserName(loginDto.getPhone());
        if(ObjectUtils.isEmpty(user)){
            return  AjaxResult.error(HttpStatus.UNAUTHORIZED,"用户名不存在，请注册");
        }
        System.out.println(user.getPassword());
        System.out.println(SecurityUtils.encryptPassword(loginDto.getPassword()));
       boolean flag= SecurityUtils.matchesPassword(loginDto.getPassword(),user.getPassword());
        if(!flag){
            return  AjaxResult.error("密码错误");
        }
        LoginWechatUser loginWechatUser = new LoginWechatUser();
        BeanUtils.copyProperties(user,loginWechatUser);
        // 生成token
        AjaxResult ajax = AjaxResult.success();
        ajax.put(Constants.TOKEN, wechatUserTokenService.createToken(loginWechatUser));
        //ajax.put(Constants.WECHAT_AUTH, openIdCheck.getUserType());
        return ajax;

    }
}
